package movie.utility;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.faces.model.SelectItem;

/**
 *
 * @author Slavko
 */
public class Utility {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
    private static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

    // STRING UTILS ================================================================================

    public static boolean isStringEmptyOrNull(String s) {
        if (s == null || s.trim().isEmpty()) {
            return true;
        }
        return false;
    }

    // DATE AND TIME  ==============================================================================
    public static String getDateString(Date date) {
        return date == null ? "" : dateFormat.format(date);
    }

    public static String getTimeString(Date date) {
        return date == null ? "" : timeFormat.format(date);
    }

    public static String getDateTimeString(Date date) {
        return date == null ? "" : dateTimeFormat.format(date);
    }

    public static Date addDays(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, amount);
        return c.getTime();
    }

    public static String getHash(String pass) {
        byte buf[] = pass.getBytes();
        String hexStr = "";
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(buf);
            byte[] digest = algorithm.digest();
            // get the hex string 
            for (int i = 0; i < digest.length; i++) {
                hexStr += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
            }
        } catch (Exception ex) {
            return "";
        }
        System.out.println(hexStr.toString());
        return hexStr.toString();
    }
}
